package whf.survey.service;

import java.util.Iterator;
import java.util.List;

import whf.framework.exception.DAOException;
import whf.framework.exception.FindException;
import whf.framework.exception.RemoveException;
import whf.framework.exception.ServiceNotFoundException;
import whf.framework.security.entity.Dept;
import whf.framework.util.BeanFactory;
import whf.survey.dao.SurveyDAO;
import whf.survey.dao.SurveyPublishDAO;
import whf.survey.dao.SurveyTopicDAO;
import whf.survey.entity.Survey;
import whf.survey.entity.SurveyPublish;
import whf.survey.entity.SurveyTopic;
import whf.survey.util.Constants;

/**
 * @author wanghaifeng
 * @email king@126.com
 * @modify 2006-09-12
 */
public class SurveyServiceImp extends whf.framework.service.ServiceImp<Survey> implements SurveyService {

	private SurveyDAO surveyDAO;
	
	private SurveyTopicDAO surveyTopicDAO;

	private SurveyPublishDAO surveyPublishDAO;
	
	/**
	 * @modify wanghaifeng Sep 12, 2006 10:26:25 PM
	 * @param surveyDAO
	 */
	public void setSurveyDAO(SurveyDAO surveyDAO) {
		this.surveyDAO = surveyDAO;
	}

	/**
	 * @param surveyTopicDAO The surveyTopicDAO to set.
	 */
	public void setSurveyTopicDAO(SurveyTopicDAO surveyTopicDAO) {
		this.surveyTopicDAO = surveyTopicDAO;
	}

	/**
	 * @param surveyPublishDAO the surveyPublishDAO to set
	 */
	public void setSurveyPublishDAO(SurveyPublishDAO surveyPublishDAO) {
		this.surveyPublishDAO = surveyPublishDAO;
	}

	public static SurveyService getSurveyService() throws ServiceNotFoundException {
		return (SurveyService)BeanFactory.getService(SurveyServiceImp.class) ;
	}

	/* (non-Javadoc)
	 * @see whf.survey.service.SurveyService#remove(whf.survey.entity.Survey)
	 */
	public void remove(Survey survey) throws RemoveException {
		String hql = "t.survey=?";
		try{
			List<SurveyTopic> topics = this.surveyTopicDAO.find(hql, survey);
			for(SurveyTopic bo : topics){
				this.surveyTopicDAO.remove((SurveyTopic)bo);
			}
		}catch(FindException e){
			throw new RemoveException(e);
		}
		this.surveyDAO.remove(survey);
	}

	/* (non-Javadoc)
	 * @see whf.survey.service.SurveyService#publish(whf.survey.entity.Survey, java.util.List)
	 */
	public void publish(Survey survey, List<Dept> depts) throws DAOException {
		List<SurveyPublish> publishs = this.surveyPublishDAO.find("t.survey=?", survey);
		for(Iterator<SurveyPublish> it = publishs.iterator(); it.hasNext(); ){
			SurveyPublish p = it.next();
			boolean exists = false;
			for(Dept dept: depts){
				if(p.getDept().idEquals(dept)){
					exists = true;
					break;
				}
			}
			if(!exists){
				it.remove();
				this.surveyPublishDAO.remove(p);
			}
		}
		for(Dept dept: depts){
			boolean exists = false;
			for(SurveyPublish p: publishs){
				if(p.getDept().idEquals(dept)){
					exists = true;
					break;
				}
			}
			if(!exists){
				SurveyPublish sp = new SurveyPublish(survey, dept);
				this.surveyPublishDAO.create(sp);
			}
		}
		//
		survey.setStatus(Constants.SURVEY_STATUS_PUBLISHED);
		this.surveyDAO.update(survey);
	}

} 